<template>
  <div ref="dragDiv" class="drag">
    <div class="drag_bg"></div>
    <div class="drag_text">{{ successStatus ? successTips : tips }}</div>
    <div
      ref="moveDiv"
      class="handler handler_bg"
      :class="{ handler_ok_bg: successStatus }"
      @touchstart="slideStart"
      @mousedown="slideStart"
    ></div>
  </div>
</template>

<script>
import { mapState, mapActions, mapMutations } from "vuex";
export default {
  name: "VerifySlider",
  props: {
    // tips: {
    //   type: String,
    //   default: "请按住滑块，拖动到最后边"
    // },
    // successTips: {
    //   type: String,
    //   default: "验证通过"
    // }
  },
  data() {
    return {
      config: {
        clientType: null,
        startEvent: null,
        moveEvent: null,
        endEvent: null,
        getClientX: null
      },
      tips: this.$t("locale.components.vueVerifySlider.t1"),
      successTips: this.$t("locale.components.vueVerifySlider.t1"),
      successStatus: false,
      beginClientX: 0 /* 距离屏幕左端距离 */,
      mouseMoveStatus: false /* 触发拖动状态  判断 */,
      maxWidth: "" /* 拖动最大宽度，依据滑块宽度算出来的 */
    };
  },
  created() {
    this.initConfig();
  },
  computed: {
    ...mapState({
      is_mobile(state) {
        return state.is_mobile;
      }
    })
  },
  mounted() {
    this.maxWidth =
      this.$refs.dragDiv.clientWidth - this.$refs.moveDiv.clientWidth;
    document
      .getElementsByTagName("html")[0]
      .addEventListener(this.config.moveEvent, this.slideMove);
    document
      .getElementsByTagName("html")[0]
      .addEventListener(this.config.endEvent, this.slideEnd);
  },
  methods: {
    slideStart(e) {
      if (this.successStatus) return;
      e.preventDefault && e.preventDefault(); // 阻止文字选中等 浏览器默认事件
      this.mouseMoveStatus = true;
      this.beginClientX = this.config.getClientX(e);
    },
    slideMove(e) {
      if (!this.mouseMoveStatus) return;
      const width = this.config.getClientX(e) - this.beginClientX;
      if (width < 0) return;
      if (width <= this.maxWidth) {
        this.$el.getElementsByClassName("handler")[0].style.left = width + "px";
        this.$el.getElementsByClassName("drag_bg")[0].style.width =
          width + "px";
      } else if (width > this.maxWidth) {
        this.successFunction();
      }
    },
    slideEnd(e) {
      this.mouseMoveStatus = false;
      const width = this.config.getClientX(e) - this.beginClientX;
      if (width < 0) return;
      if (width < this.maxWidth) {
        this.$el.getElementsByClassName("handler")[0].style.left = 0 + "px";
        this.$el.getElementsByClassName("drag_bg")[0].style.width = 0 + "px";
      }
    },
    successFunction() {
      this.successStatus = true;
      this.$emit("success");
      document
        .getElementsByTagName("html")[0]
        .removeEventListener(this.config.moveEvent, this.slideMove);
      document
        .getElementsByTagName("html")[0]
        .removeEventListener(this.config.endEvent, this.slideEnd);
      document.getElementsByClassName("drag_text")[0].style.color = "#606266";
      document.getElementsByClassName("handler")[0].style.left =
        this.maxWidth + "px";
      document.getElementsByClassName("drag_bg")[0].style.width =
        this.maxWidth + "px";
    },
    getClientX(e) {
      if (this.config.clientType === "mobile") {
        return e.changedTouches[0].clientX;
      } else {
        return e.clientX;
      }
    },
    initConfig() {
      // this.config.clientType = navigator.userAgent.match(
      //   /(iPhone|iPod|Android|ios)/i
      // )
      //   ? "mobile"
      //   : "pc";
      let is_macOS = navigator.userAgent.match(/Macintosh/i);
      if (this.is_mobile) {
        this.config.clientType = "mobile";
      } else {
        //iPad设备上都是用mobile事件
        if (is_macOS) {
          this.config.clientType = "mobile";
        } else {
          this.config.clientType = "pc";
        }
      }
      if (this.config.clientType === "mobile") {
        //兼容ipad上 请求桌面网站
        if ("ontouchstart" in window) {
          this.config.startEvent = "touchstart";
          this.config.moveEvent = "touchmove";
          this.config.endEvent = "touchend";
          this.config.getClientX = e => e.changedTouches[0].clientX;
        } else if ("onmousedown" in window) {
          this.config.startEvent = "mousedown";
          this.config.moveEvent = "mousemove";
          this.config.endEvent = "mouseup";
          this.config.getClientX = e => e.clientX;
        }
      } else {
        this.config.startEvent = "mousedown";
        this.config.moveEvent = "mousemove";
        this.config.endEvent = "mouseup";
        this.config.getClientX = e => e.clientX;
      }
    },
    initComponent() {
      this.initConfig();
      this.successStatus = false;
      this.$el.getElementsByClassName("handler")[0].style.left = "0px";
      this.$el.getElementsByClassName("drag_bg")[0].style.width = "0px";
      document
        .getElementsByTagName("html")[0]
        .addEventListener(this.config.moveEvent, this.slideMove);
      document
        .getElementsByTagName("html")[0]
        .addEventListener(this.config.endEvent, this.slideEnd);
    }
  }
};
</script>

<style lang="scss" scoped>
* {
  box-sizing: border-box;
}

.drag {
  position: relative;
  background-color: rgb(250, 250, 250);
  width: 290px;
  height: 40px;
  line-height: 38px;
  text-align: center;
  border-radius: 4px;
}

.handler {
  position: absolute;
  top: 0;
  left: 0;
  width: 40px;
  height: 40px;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  cursor: move;
}

.handler_bg {
  background: #fff
    url("")
    no-repeat center;
}

.handler_ok_bg {
  background: #fff
    url("")
    no-repeat center;
}

.drag_bg {
  background-color: #e2f3d9;
  border: 1px solid #d4edc6;
  border-right: 0;
  height: 40px;
  width: 0;
}

.drag_text {
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  position: absolute;
  top: 0;
  width: 100%;
  font-size: 14px;
  text-align: center;
  -moz-user-select: none;
  -webkit-user-select: none;
  user-select: none;
  -o-user-select: none;
  -ms-user-select: none;
}
</style>
